#include "include/debug.h"
#include "include/filehandle.h"

extern byte codes[0x1000000];
extern byte datas[0x1000000];

extern qword iregs[128];
extern float fregs[64];
extern double dregs[64];

#if defined(__debug__)
int main()
{
    init();
    fHeader fileInfo;
    bool hasCode = false;
    char value = '\0';
    Pair ptr = {0, 0};
    while (value != 'o')
    {
        std::string filename;
        std::cout << "Input filename:";
        std::cin >> filename;
        fileInfo = getHeader(filename.c_str());
        getPara(filename.c_str(), fileInfo);
        ptr = *loadImage(filename.c_str(), &ptr);
        if (fileInfo.fType == 0)
            hasCode = true;
        if ((fileInfo.fType != 0) && (hasCode == false))
            printf("****ATTENTION****\nThere is only data files,we recommand you to load a code file.\nYou can also choose run without code files,press \'o\' to continue,or press other keys to load another file.\n");
        else
            printf("Code file has loaded,press \'o\' to continue,else press other keys to select more files.\n");
        getchar();
        value = getchar();
    }
    while (1)
        dmain();
    return 0;
}
#else
int main(int argc, char **argv)
{
    init();
    if (argc < 2)
    {
        std::cout << "fatal error:no file used.\n";
        exit(1);
    }
    else
    {
        getpHeader(argv[1], getHeader(argv[1]));
        loadImage(argv[1]);
        while ((iregs[Flag] & RUNNING) && !(iregs[Flag] & (ERROR + DEBUG)))
            debug(argc - 2, argv + 2);
    }
}
#endif